package com.cadre.web.controller.officer;

import com.cadre.common.utils.R;
import com.cadre.common.utils.common.GenerateUUID;
import com.cadre.system.domain.officer.A01;
import com.cadre.system.domain.officer.A17;
import com.cadre.system.domain.vo.ESUtil;
import com.cadre.system.service.A17Service;
import com.alibaba.fastjson2.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = {"正向激励、负面评价"})
@RequestMapping({"/profess/a17"})
@RestController
/* loaded from: A17Controller.class */
public class A17Controller {
    @Resource
    private A17Service a17Service;
    @Autowired
    RestHighLevelClient client;

    @GetMapping({"/list"})
    @ApiOperation("由人员personid获取信息")
    public R list(String personid) {
        Map<String, Object> map = new HashMap<>();
        map.put("personid", personid);
        return R.ok().put("data", (Object) this.a17Service.selectByList(map));
    }

    @PostMapping({"/editA17Msg/{personid}"})
    @ApiOperation("操作A17")
    public R editA16Msg(@RequestBody List<A17> list, @PathVariable("personid") String personid) throws IOException {
        int value;
        if (list.size() > 0) {
            personid = list.get(0).getPersonid();
            if (!(personid == null || personid == "")) {
                this.a17Service.deleteByPersonId(personid);
            }
            for (int i = 0; i < list.size(); i++) {
                A17 a17 = list.get(i);
                a17.setFlowid(GenerateUUID.getUUID());
                if (a17.getA1702().equals("减")) {
                    value = 80 - Integer.valueOf(a17.getA1703()).intValue();
                } else {
                    value = 80 + Integer.valueOf(a17.getA1703()).intValue();
                }
                a17.setA1705(String.valueOf(value));
                this.a17Service.insertSelective(a17);
            }
        } else if (!(personid == null || personid == "")) {
            this.a17Service.deleteByPersonId(personid);
        }
        Map<String, Object> source = this.client.get(new GetRequest(ESUtil.a01Index, personid), RequestOptions.DEFAULT).getSource();
        if (source != null) {
            A01 a01s = ESUtil.useObjectMapper(source);
            a01s.setA17List(list);
            this.client.index(new IndexRequest(ESUtil.a01Index).id(a01s.getFlowid()).source(JSON.toJSONString(a01s), XContentType.JSON), RequestOptions.DEFAULT);
        }
        return R.ok();
    }
}
